package com.activity.gaswatchermtl.fragment;

import java.util.ArrayList;
import java.util.List;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.activity.Interface.LoginActivity;
import com.activity.gaswatchermtl.R;
import com.activity.gaswatchermtl.StartActivity;
import com.activity.gaswatchermtl.StationDetailsActivity;
import com.inf8405.db.DAOStations;
import com.inf8405.db.SQLiteHelper;

public class FragmentTabFavorite extends Fragment implements OnClickListener {
 
	private View inflatedView_ = null;
	Button btn_save;
	ListView listView;
	
	List<String> list_reference_fv = new ArrayList<String>();
	List<String> list_name_fv = new ArrayList<String>();
	List<String> list_address_fv = new ArrayList<String>();
	List<String> list_price_fv = new ArrayList<String>();
	static String[] arr_reference_fv ;
	static String[] arr_name_fv ;
	static String[] arr_address_fv;
	static String[] arr_price_fv;
	Button button_refresh;
	Button button_delete;
	
		
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    	if (inflatedView_ == null){
			inflatedView_ = inflater.inflate(R.layout.fragment_station_favorite, container, false);	
		}
   	
    	readDBdata() ;
		listView = (ListView) inflatedView_.findViewById(R.id.station_fv_list);
		listView.setAdapter(new EfficientAdapter(getActivity()));
		
		button_delete = (Button) inflatedView_.findViewById(R.id.btn_delete_list_favorite);
		button_refresh = (Button) inflatedView_.findViewById(R.id.btn_refresh_list_favorite);
		button_delete.setOnClickListener(this);
		button_refresh.setOnClickListener(this);

	    if (listView != null) {
	    	listView.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position,
						long id) {
					String reference = ((TextView) view.findViewById(R.id.reference_fav)).getText().toString();
					Intent in = new Intent(getActivity().getApplicationContext(), StationDetailsActivity.class);
					in.putExtra(StationDetailsActivity.KEY_REFERENCE, reference);
					startActivity(in);
				}
			});
		}
    	return inflatedView_;
    }

    @Override
    public void onClick(View v) {
      if( v==button_delete )
      {
    	  getActivity().getApplicationContext().deleteDatabase(SQLiteHelper.DATABASE_NAME);
    	  readDBdata() ;
  		  listView.setAdapter(new EfficientAdapter(getActivity()));
      }
       
      if( v==button_refresh )
      {
    	  readDBdata() ;
  		  listView.setAdapter(new EfficientAdapter(getActivity()));
      }
        
      
    }

    public void readDBdata() {
		SQLiteHelper dbh = new SQLiteHelper(getActivity());
		SQLiteDatabase db = dbh.getWritableDatabase();
		list_reference_fv.clear();
		list_name_fv.clear();
		list_address_fv.clear();
		list_price_fv.clear();

		Cursor cursor = db.rawQuery("Select " + dbh.COLUMN_REFERENCE + "," + dbh.COLUMN_NAME + "," + dbh.COLUMN_FORMATTED_ADDRESS + ","
				+ dbh.COLUMN_PRICE +" from "+dbh.TABLE_NAME+";", null);

		if (cursor.moveToFirst()) {
			do {
				list_reference_fv.add(cursor.getString(0));
				list_name_fv.add(cursor.getString(1));
				list_address_fv.add(cursor.getString(2));
				list_price_fv.add(cursor.getString(3));
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		db.close();
		arr_reference_fv = new String[list_reference_fv.size()];
		arr_name_fv = new String[list_name_fv.size()];
		arr_address_fv = new String[list_address_fv.size()];
		arr_price_fv = new String[list_price_fv.size()];

		for (int i = 0; i < list_name_fv.size(); i++) {
			arr_reference_fv[i] = list_reference_fv.get(i);
			arr_name_fv[i] = list_name_fv.get(i);
			arr_address_fv[i] = list_address_fv.get(i);
			arr_price_fv[i] = list_price_fv.get(i);
		}
	}

	private static class EfficientAdapter extends BaseAdapter {
		private LayoutInflater mInflater;

		public EfficientAdapter(Context context) {
			mInflater = LayoutInflater.from(context);
		}

		public int getCount() {
			return arr_price_fv.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.three_col_row, null);
				holder = new ViewHolder();
				holder.text0 = (TextView) convertView
						.findViewById(R.id.reference_fav);
				holder.text1 = (TextView) convertView
						.findViewById(R.id.name_fav_stat);
				holder.text2 = (TextView) convertView
						.findViewById(R.id.addr_fav_stat);
				holder.text3 = (TextView) convertView
						.findViewById(R.id.price_fav_stat);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.text0.setText(arr_reference_fv[position]);
			holder.text1.setText(arr_name_fv[position]);
			holder.text2.setText(String.valueOf(arr_address_fv[position] ));
			holder.text3.setText(String.valueOf(arr_price_fv[position] ));
			
			return convertView;
		}

		static class ViewHolder {
			TextView text0;
			TextView text1;
			TextView text2;
			TextView text3;
		}
	}

}
